<template>
  <!-- 计算公式维护编辑 -->
  <base-breadcrumb :title="$route.meta.title" :breadcrumb="!isAudit">
    <a-spin :spinning="loading">
      <a-card style="margin-bottom:80px">
        <!--        <a-anchor>-->
        <a-tabs
          :activeKey="currentStep"
          @change="callback"
        >
          <a-tab-pane
            key="1"
            tab="项目基本情况"
          >
            <projectInfo
                    :isAudit="isAudit"
              :form="form"
              @nextStep="changeStep('2')"
            ></projectInfo>
          </a-tab-pane>
          <a-tab-pane
            key="2"
            tab="目标完成情况"
          >
            <targetComplete
              :form="form"
              @previousStep="changeStep('1')"
              @nextStep="changeStep('3')"
              :isAudit="isAudit"
            ></targetComplete>
          </a-tab-pane>
          <a-tab-pane
            key="3"
            tab="项目策划执行情况"
          >
            <planningPerform
              :form="form"
              @previousStep="changeStep('2')"
              @nextStep="changeStep('4')"
              :isAudit="isAudit"
            ></planningPerform>
          </a-tab-pane>
          <a-tab-pane
            key="4"
            tab="重要节点情况"
          >
            <nodeCompletion
              :form="form"
              @previousStep="changeStep('3')"
              @nextStep="changeStep('5')"
              :isAudit="isAudit"
            ></nodeCompletion>
          </a-tab-pane>
          <a-tab-pane
            key="5"
            tab="下月重点安排"
          >
            <nextMonthArrange
              :form="form"
              @previousStep="changeStep('4')"
              @nextStep="changeStep('6')"
              :isAudit="isAudit"
            ></nextMonthArrange>
          </a-tab-pane>
          <a-tab-pane
            key="6"
            tab="附件"
          >
            <attachment
                    :isAudit="isAudit"
              :form="form"
              @previousStep="changeStep('5')"
            ></attachment>
          </a-tab-pane>
        </a-tabs>
        <!--        </a-anchor>-->
      </a-card>
      <!-- 页脚操作栏 -->
      <div
        class="btn_box tool-btns--fixed page-btn-right-top"
        v-if="editModel != 0"
      >
        <a-button
          class="button_right"
          v-if="form.auditStatus == 'REJECTED'"
          type="primary"
          :loading="loadingBtn.REJECTED"
          @click="handleSubmit('REJECTED')"
        >保存</a-button>
        <a-button
          v-else
          class="button_right"
          type="primary"
          :loading="loadingBtn.DRAFT"
          @click="handleSubmit('DRAFT')"
        >保存草稿</a-button>
        <a-button
          class="button_right"
          type="primary"
          :loading="loadingBtn.SUBMITTED"
          @click="handleSubmit('SUBMITTED')"
        >提交</a-button>
        <a-button @click="handleActiveClose">关闭</a-button>
      </div>
      <div
        class="btn_box tool-btns--fixed page-btn-right-top"
        v-else-if="!isConsultation"
      >
        <a-button @click="handleActiveClose">关闭</a-button>
      </div>
    </a-spin>
    <!-- 选择审批人 -->
    <selectApprover
      :business-id="businessId"
      :process-config="setProcessConfig"
    />
    <!-- 选择审批人 -->
  </base-breadcrumb>
</template>

<script>
import { api } from '@/api/project/performanceAnalysis'
import BaseEditPage from '@/views/baseEditPage' // 编辑详情页基础配置
import BSelect from '@/components/BComponents/BSelect'
import SelectSubCompany from '@/components/Select/SelectSubCompany' // 分公司
import SelectProject from '@/components/Select/SelectProject' // 分公司
import projectInfo from './components/projectInfo'
import targetComplete from './components/targetComplete'
import planningPerform from './components/planningPerform'
import nodeCompletion from './components/nodeCompletion'
import nextMonthArrange from './components/nextMonthArrange'
import attachment from './components/attachment'

export default {
  name: 'updateProjectPerformanceAnalysis',
  extends: new BaseEditPage(),
  components: {
    SelectProject,
    BSelect,
    SelectSubCompany,
    projectInfo,
    targetComplete,
    planningPerform,
    nodeCompletion,
    nextMonthArrange,
    attachment
  },
  props: {
    // 是否作为审批详情
    isAudit: {
      type: Boolean,
      default: false
    },
    // 是否作为意见征询
    isConsultation: {
      type: Boolean,
      default: false
    }
  },
  data() {
    return {
      loading: false, // 加载控件
      editModel: 1, // 操作状态:0 查看;1 编辑
      form: {}, // 表单
      id: '',
      currentStep: '1',
      loadingBtn: {
        DELETE: false,
        DRAFT: false,
        SUBMITTED: false,
        REJECTED: false
      },

      rules: { month: [{ required: true, message: '请选择' }] }, // 校验规则
      init: false, // 初始化控件
      // 流程配置
      processConfig: null,
      //点击提交发送流程配置值
      setProcessConfig: null,
      // 业务id
      businessId: null
    }
  },
  watch: {
    isAudit: {
      immediate: true,
      handler(val) {
        if (val) {
          this.$nextTick(() => {
            this.editModel = 0
          })
        }
      }
    }
  },
  async created() {
    //获取通过获取流程配置值
    this.processConfig = await this.$getConfigFn('project_lvyue_project')
  },
  mounted() {
    // 不为添加时请求详情
    if(!this.isAudit){
      this.$route.meta.title = '项目部工程履约分析'
    }
    if (this.editModel != 2) {
      this.getDetailData()
    } else {
      this.$nextTick(() => {
        // 默认映射联系人为登陆人

        this.init = true
      })
    }
  },

  methods: {
    callback(step) {
      this.currentStep = step
    },
    changeStep(nextStep) {
      this.currentStep = nextStep
    },

    getDetailData() {
      this.loading = true
      api
        .getLvyueProject({ id: this.id })
        .then((res) => {
          this.loading = false
          this.form = { ...res.data }
          this.form.keyrateVO.projectOrgId = this.form.projectVO.projectOrgId
          this.form.keyrateVO.branchCompanyOrgId = this.form.projectVO.branchCompanyId
          console.log(this.form)
          this.$nextTick(() => {
            this.init = true
          })
        })
        .catch(() => {
          this.loading = false
        })
    },

    /**
     * 提交表单
     */
    validateForm() {
      let flag1 = true,
        flag2 = true,
        flag3 = true,
        flag4 = true,
        flag5 = true,
        flag6 = true,
        flag7 = true,
        flag8 = true

      //校验形象进度缺失
      if (!this.form.projectVO.progress) {
        this.$message.warning('形象进度缺失，请先填写该月份的项目动态信息')
        flag1 = false
      }
      //校验资源配置情况
      if (this.form.projectVO.projectManager) {
        this.form.projectVO.detailsList.forEach((i) => {
          if (!i.SJManager) {
            flag2 = false
          }
        })
      }
      if (this.form.projectVO.projectRecordManager) {
        this.form.projectVO.detailsList.forEach((i) => {
          if (!i.BAManager) {
            flag2 = false
          }
        })
      }
      //校验关键绩效指标完成情况
      this.form.keyrateVO.keyrateDTOList?.forEach((i) => {
        if (i.indexCode == 101 || i.indexCode == 102 || i.indexCode == 601) {
          if (!i.value) {
            if (i.indexCode == 601) {
              if (!(this.editModel == 0 || i.nocheck == 1)) {
                flag3 = false
              }
            } else {
              flag3 = false
            }
          }
        }
      })

      //校验风险情况统计
      if (this.form.keyrateVO.riskDTOList?.length > 0) {
        let list = ['analysis', 'takeSteps', 'complete', 'branchCompanyDutyId', 'projectDutyId', 'endTime']
        this.form.keyrateVO.riskDTOList?.forEach((obj) => {
          Object.keys(obj).forEach((key) => {
            if (list.indexOf(key) != -1 && !obj[key]) {
              flag4 = false
            }
          })
        })
      } else {
        flag4 = false
      }
      if (this.form.planproblemVO.planproblemVosNode?.length > 0) {
        let list = ['problem', 'correctiveAction', 'completeTime']
        this.form.planproblemVO.planproblemVosNode.forEach((obj) => {
          Object.keys(obj).forEach((key) => {
            if (list.indexOf(key) != -1 && !obj[key]) {
              flag5 = false
            }
          })
        })
      } else {
        flag5 = false
      }
      if (this.form.planproblemVO.planproblemVosSub?.length > 0) {
        let list = ['problem', 'correctiveAction', 'completeTime']
        this.form.planproblemVO.planproblemVosSub.forEach((obj) => {
          Object.keys(obj).forEach((key) => {
            if (list.indexOf(key) != -1 && !obj[key]) {
              flag6 = false
            }
          })
        })
      } else {
        flag6 = false
      }
      if (this.form.planproblemVO.planproblemVosTechnology?.length > 0) {
        let list = ['problem', 'correctiveAction', 'completeTime']
        this.form.planproblemVO.planproblemVosTechnology.forEach((obj) => {
          Object.keys(obj).forEach((key) => {
            if (list.indexOf(key) != -1 && !obj[key]) {
              flag7 = false
            }
          })
        })
      } else {
        flag7 = false
      }
      if(!!Number((this.form.keyrateVO?.keyrateDTOList || [])[1]?.value)){
        if((this.form.ownerFileInfoVOS || []).length<=0){
          flag8 = false
        }
      }
      if (!flag2) {
        this.$message.warning('资源配置情况表格输入不完整')
      }
      if (!flag3) {
        this.$message.warning('关键绩效指标完成情况表格输入不完整')
      }
      if (!flag4) {
        this.$message.warning('风险情况统计表格输入不完整')
      }
      if (!flag5) {
        this.$message.warning('节点、劳动力管控问题表格输入不完整')
      }
      if (!flag6) {
        this.$message.warning('分包、分供招采问题表格输入不完整')
      }
      if (!flag7) {
        this.$message.warning('技术、设计问题表格输入不完整')
      }
      if (!flag8) {
        this.$message.warning('请上传业主确认延期佐证资料')
      }
      if (flag1 && flag2 && flag3 && flag4 && flag5 && flag6 && flag7 && flag8) {
        return true
      } else {
        return false
      }
    },
    formatList() {
      if (this.form.projectVO.projectManager) {
        this.form.projectVO.projectManager.age = this.form.projectVO.detailsList[1].SJManager
        this.form.projectVO.projectManager.workNum = this.form.projectVO.detailsList[2].SJManager
        this.form.projectVO.projectManager.managerNum = this.form.projectVO.detailsList[3].SJManager
        this.form.projectVO.projectManager.workRigs = this.form.projectVO.detailsList[4].SJManager
      }
      if (this.form.projectVO.projectRecordManager) {
        this.form.projectVO.projectRecordManager.age = this.form.projectVO.detailsList[1].BAManager
        this.form.projectVO.projectRecordManager.workNum = this.form.projectVO.detailsList[2].BAManager
        this.form.projectVO.projectRecordManager.managerNum = this.form.projectVO.detailsList[3].BAManager
        this.form.projectVO.projectRecordManager.workRigs = this.form.projectVO.detailsList[4].BAManager
      }
    },
    /**
     * 提交表单
     */
    handleSubmit(status) {
      if (status == 'SUBMITTED' && !this.validateForm()) {
        return
      }

      this.formatList()

      let form = this.$clone(this.form)
      this.loading = true
      form.auditStatus = status
      this.setProcessConfig = null
        this.submitApprovalConfirm(status == 'SUBMITTED' ? this.processConfig : 6, (flag) => {
          if(!flag) {
            this.loading = false
            return
          }
          api.updateLvyueProject(form)
                  .then((res) => {
                    console.log(res)
                    if (res.code == 200) {
                      this.loading = false
                      this.$message.success(status == 'SUBMITTED' ? '提交成功！' : '保存成功')
                      if (status == 'SUBMITTED') {
                        this.businessId = res.data.projectVO.id
                        this.setProcessConfig = this.processConfig
                      } else {
                        this.$multiTab.close()
                      }
                    } else {
                      this.loading = false
                    }
                  })
                  .catch(() => {
                    this.loading = false
                    this.$message.warning(err.data.message)
                  })
        })
    }
  }
}
</script>
<style lang="less" scoped>
/deep/ .form-item  {
  .ant-input:not([disabled]),
  .ant-select:not(.ant-select-disabled) .ant-select-selection,
  .ant-calendar-picker-input,
  .ant-input-number:not([disabled]),
  .ant-cascader-picker {
    border-color: #e4ac0b;

    background-color: #fff9e8;
  }
  .ant-input-disabled,
  .ant-input-number-disabled .ant-input-number-input,
  .ant-time-picker-input[disabled] {
    border-color: #d9d9d9 !important;
    background-color: #f5f5f5 !important;
  }

  .remark {
    .ant-input {
      border-color: inherit;
      background-color: initial;
    }
  }
}
</style>
